package com.shujia.mysql;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
* @Description: MysqlJDBC2 使用PreparedStatement添加
* @Author: 王火火
* @Date: 2019/9/6 14:13
*/
public class MysqlJDBC2 {
    //创建常量
    static final String DRIVER = "com.mysql.jdbc.Driver";//驱动
    static final String URL = "jdbc:mysql://master:3306/shujia";//路劲
    static final String USERNAME = "root";//用户名
    static final String PASSWORD = "123456";//密码

    public static void main(String[] args){
        //加载驱动
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        //获取连接
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //sql语句
        String sql="insert into emp(empno,ename,job,mgr,hiredate,sal,comm,deptno) " +
                "values(12,'test','job',5000,'1999-03-03',5000,100,20)";
        //获取执行器
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        //开始执行
        int i = 0;
        try {
            i = ps.executeUpdate();
        } catch (SQLException e) {
            System.out.println("添加失败");
        }
        if(i==1){
            System.out.println("添加成功");
        }

        try {
            ps.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
